<template>
  <BaseLayout>
    <!-- 顶部菜单栏 -->
    <div class="layout__header">
      <div class="layout__header-content">
        <!-- Logo区域 -->
        <div v-if="isShowLogo" class="layout__header-logo">
          <AppLogo :collapse="isLogoCollapsed" />
        </div>

        <!-- 顶部菜单区域 -->
        <div class="layout__header-menu">
          <MixTopMenu />
        </div>

        <!-- 右侧操作区域 -->
        <div class="layout__header-actions">
          <NavbarActions />
        </div>
      </div>
    </div>

    <!-- 主内容区容器 -->
    <div class="layout__container">
      <!-- 左侧菜单栏 -->
      <div
        v-if="sideMenuRoutes.length > 1"
        class="layout__sidebar--left"
        :class="{ 'layout__sidebar--collapsed': !isSidebarOpen }"
      >
        <el-scrollbar>
          <el-menu
            :default-active="activeLeftMenuPath"
            :collapse="!isSidebarOpen"
            :collapse-transition="false"
            :unique-opened="false"
            :background-color="variables['menu-background']"
            :text-color="variables['menu-text']"
            :active-text-color="variables['menu-active-text']"
          >
            <MenuItem
              v-for="item in sideMenuRoutes"
              :key="item.path"
              :item="item"
              :base-path="resolvePath(item.path)"
            />
          </el-menu>
        </el-scrollbar>
        <!-- 侧边栏切换按钮 -->
        <div class="layout__sidebar-toggle">
          <Hamburger :is-active="isSidebarOpen" @toggle-click="toggleSidebar" />
        </div>
      </div>

      <!-- 主内容区 -->
      <div :class="{ hasTagsView: isShowTagsView }" class="layout__main">
        <TagsView v-if="isShowTagsView" />
        <AppMain />
      </div>
    </div>
  </BaseLayout>
</template>

<script setup lang="ts">
import { useRoute } from "vue-router";
import { useWindowSize } from "@vueuse/core";
import { useLayout } from "../composables/useLayout";
import { useLayoutMenu } from "../composables/useLayoutMenu";
import BaseLayout from "./BaseLayout.vue";
import AppLogo from "../components/AppLogo/index.vue";
import MixTopMenu from "../components/Menu/MixTopMenu.vue";
import NavbarActions from "../components/NavBar/components/NavbarActions.vue";
import TagsView from "../components/TagsView/index.vue";
import AppMain from "../components/AppMain/index.vue";
import MenuItem from "../components/Menu/components/MenuItem.vue";
import Hamburger from "@/components/Hamburger/index.vue";
import variables from "@/styles/variables.module.scss";
import { isExternal } from "@/utils/index";
import { computed, watch } from "vue";
import { useAppStore, usePermissionStore } from "@/store";

const route = useRoute();
console.log("总路由", route);

// 根据视口宽度动态设置根字体大小
function setRem() {
  const baseSize = 16; // 基准值16px
  const scale = document.documentElement.clientWidth / 1920; // 假设设计稿宽度为1920px
  document.documentElement.style.fontSize = baseSize * Math.min(scale, 2) + "px";
}

// 布局相关参数
const { isShowTagsView, isShowLogo, isSidebarOpen, toggleSidebar } = useLayout();

// 菜单相关
const { sideMenuRoutes, activeTopMenuPath } = useLayoutMenu();
console.log("菜单数据", sideMenuRoutes.value);
// 响应式窗口尺寸
const { width } = useWindowSize();

// 只有在小屏设备（移动设备）时才折叠Logo（只显示图标，隐藏文字）
const isLogoCollapsed = computed(() => width.value < 768);

// 当前激活的菜单
const activeLeftMenuPath = computed(() => {
  console.log("我的路由", route);
  const { meta, path } = route;

  console.log("我的路由", meta, path);
  // 如果设置了activeMenu，则使用
  if (meta?.activeMenu && typeof meta.activeMenu === "string") {
    return meta.activeMenu;
  }
  return path;
});

/**
 * 解析路径 - 混合模式下，左侧菜单是从顶级菜单下的子菜单开始的
 * 所以需要拼接顶级菜单路径
 */
function resolvePath(routePath: string) {
  console.log("我的123212路由路径", routePath);

  if (isExternal(routePath)) {
    return routePath;
  }

  if (routePath.startsWith("/")) {
    console.log("111111", routePath, activeTopMenuPath.value + routePath);
    return activeTopMenuPath.value + routePath;
  }
  console.log("122222", `${activeTopMenuPath.value}/${routePath}`);

  return `${activeTopMenuPath.value}/${routePath}`;
}
// 初始化
setRem();
// 窗口大小变化时重新计算
window.addEventListener("resize", setRem);

// 监听路由变化，确保左侧菜单能随TagsView切换而正确激活
watch(
  () => route.path,
  (newPath) => {
    console.log("新路由", newPath);
    // 获取顶级路径
    const matchTop = typeof newPath === "string" ? newPath.match(/^\/[^/]+/) : null;
    const topMenuPath =
      typeof newPath === "string" && newPath.split("/").filter(Boolean).length > 1
        ? matchTop
          ? matchTop[0]
          : "/"
        : "/";

    // 如果当前路径属于当前激活的顶部菜单
    if (newPath.startsWith(activeTopMenuPath.value)) {
      console.log("📍 Route is under active top menu, ensuring menu item is activated");
    }
    // 如果路径改变了顶级菜单，确保顶部菜单和左侧菜单都更新
    else if (topMenuPath !== activeTopMenuPath.value) {
      console.log(
        "📍 Top menu changed, updating active menu from:",
        activeTopMenuPath.value,
        "to:",
        topMenuPath
      );

      // 主动更新顶部菜单和左侧菜单
      const appStore = useAppStore();
      const permissionStore = usePermissionStore();

      appStore.activeTopMenu(topMenuPath);
      permissionStore.updateSideMenu(topMenuPath);
    }
  },
  { immediate: true }
);
</script>

<style lang="scss" scoped>
html {
  font-size: 16px;
}
.layout {
  &__header {
    position: sticky;
    top: 0;
    z-index: 999;
    width: 100%;
    height: $navbar-height;
    // 使用 #3060ff 为主的渐变背景
    background: linear-gradient(135deg, #3060ff 0%, #1e40af 100%);
    // 添加阴影效果
    box-shadow:
      0 0.25rem 1.25rem rgba(48, 96, 255, 0.2),
      0 0.0625rem 0.25rem rgba(48, 96, 255, 0.1);
    border-bottom: none;
    // 添加毛玻璃效果
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);

    &-content {
      display: flex;
      align-items: center;
      height: 100%;
      padding: 0;
    }

    &-logo {
      display: flex;
      flex-shrink: 0;
      align-items: center;
      justify-content: center;
      height: 100%;
      padding: 0 1.25rem;
      // Logo区域背景
      background: rgba(255, 255, 255, 0.1);
      border-radius: 0 0.3125rem 0.3125rem 0;
      margin-right: 1.25rem;
      transition: all 0.3s ease;

      &:hover {
        background: rgba(255, 255, 255, 0.15);
        transform: translateY(-1px);
      }
    }

    &-menu {
      display: flex;
      flex: 1;
      align-items: center;
      min-width: 0;
      height: 100%;
      overflow: hidden;

      :deep(.el-menu) {
        height: 100%;
        background-color: transparent;
        border: none;
      }

      :deep(.el-menu--horizontal) {
        display: flex;
        align-items: center;
        height: 100%;

        .el-menu-item {
          height: 70% !important; // 调整高度为70%，减少上下空间
          line-height: normal; // 使用normal让文字自然居中
          border-bottom: none;
          background-color: transparent;
          color: rgba(255, 255, 255, 0.9);
          font-weight: 500;
          margin: 0 0.25rem;
          // font-weight: 600;

          border-radius: 0.25rem;
          font-size: 1rem;
          transition: all 0.3s ease;
          position: relative;
          overflow: hidden;
          display: flex !important; // 强制使用flex布局
          align-items: center !important; // 垂直居中对齐

          &::before {
            content: "";
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
            transition: left 0.5s;
          }

          &:hover {
            background: rgba(255, 255, 255, 0.15);
            color: #fff;
            transform: translateY(-2px);
            box-shadow: 0 0.25rem 0.75rem rgba(48, 96, 255, 0.3);

            &::before {
              left: 100%;
            }
          }

          &.is-active {
            background: #fff !important; // 纯白色背景
            color: #3060ff !important; // 文字颜色改为主题蓝色
            font-weight: bold;
            border-bottom: 0.1875rem solid #3060ff;
            box-shadow: 0 0.125rem 0.5rem rgba(48, 96, 255, 0.2);
            border-radius: 0.3125rem;
            font-size: 1rem;
            margin: 0 0.25rem;
            font-weight: 600;

            padding: 0 1rem;
            transform: scale(0.95);
            height: 60% !important; // 激活状态时高度更小
          }
        }
      }
    }

    &-actions {
      display: flex;
      flex-shrink: 0;
      align-items: center;
      height: 100%;
      padding: 0 1.25rem;

      // 操作按钮区域样式
      :deep(.navbar-actions__item) {
        margin: 0 0.5rem;
        padding: 0.5rem;
        border-radius: 0.5rem;
        transition: all 0.3s ease;

        &:hover {
          background: rgba(255, 255, 255, 0.1);
          transform: translateY(-1px);
        }
      }
    }
  }

  &__container {
    display: flex;
    height: calc(100vh - $navbar-height);
    padding-top: 0;

    .layout__sidebar--left {
      position: relative;
      width: $sidebar-width;
      height: 100%;
      // 现代化侧边栏背景
      // background: linear-gradient(180deg, #f8fafc 0%, #e2e8f0 100%);
      background-color: #ffffff;
      // 添加边框和阴影
      border-right: 0.0625rem solid rgba(48, 96, 255, 0.1);
      box-shadow: 0.25rem 0 1.25rem rgba(48, 96, 255, 0.08);
      transition: all 0.3s ease;

      &.layout__sidebar--collapsed {
        width: $sidebar-width-collapsed !important;
      }

      :deep(.el-scrollbar) {
        height: calc(100vh - $navbar-height - 60px);
        padding: 0.625rem;
      }

      :deep(.el-menu) {
        height: 100%;
        border: none;
        background: transparent;
        .el-menu-item {
          // height: 80%;
          // line-height: calc($navbar-height * 0.8);
          height: 3.2rem;
          line-height: 3.2rem;
          border-bottom: none;
          background-color: transparent;
          color: #5c5c5c; // 修改未选中时的字体颜色为灰蓝色
          font-weight: 500;

          font-size: 0.937rem;
          margin: 0.5rem 0.2rem;
          border-radius: 0.2rem;
          transition: all 0.3s ease;
          position: relative;
          overflow: hidden;
          display: flex;
          align-items: center;

          // &::before {
          //   content: "";
          //   position: absolute;
          //   left: 0;
          //   top: 0;
          //   width: 0.25rem;
          //   height: 100%;
          //   background: #3060ff;
          //   transform: scaleY(0);
          //   transition: transform 0.3s ease;
          // }

          &:hover {
            background: linear-gradient(
              135deg,
              rgba(48, 96, 255, 0.1) 0%,
              rgba(48, 96, 255, 0.05) 100%
            );
            color: #3060ff; // 悬停时变为主题蓝色
            transform: translateX(4px);
            // box-shadow: 0 0.25rem 0.75rem rgba(48, 96, 255, 0.15);

            &::before {
              transform: scaleY(1);
            }
          }

          &.is-active {
            // background: linear-gradient(135deg, #3060ff 0%, #1e40af 100%);
            background-color: rgba(48, 96, 255, 0.1);
            color: #3060ff; // 选中时为白色
            // box-shadow: 0 0.25rem 1rem rgba(48, 96, 255, 0.1);

            &::before {
              transform: scaleY(1);
              background: #fff;
            }
          }
        }

        .el-sub-menu {
          .el-sub-menu__title {
            height: 3.2rem;
            line-height: 3.2rem;
            border-bottom: none;
            background-color: transparent;
            color: #5c5c5c; // 修改未选中时的字体颜色为灰蓝色
            font-weight: 500;
            font-size: 0.937rem;
            margin: 0.5rem 0.2rem;
            border-radius: 0.2rem;
            transition: all 0.3s ease;
            position: relative;
            overflow: hidden;
            display: flex;
            align-items: center;

            &:hover {
              background: linear-gradient(135deg, #f1f5f9 0%, #e2e8f0 100%);
              color: #3060ff;
              transform: translateX(2px);
            }
          }
        }
      }

      .layout__sidebar-toggle {
        position: absolute;
        bottom: 0;
        display: flex;
        align-items: center;
        justify-content: center;
        width: 100%;
        height: 3.75rem;
        line-height: 3.75rem;
        // 切换按钮区域美化
        background: linear-gradient(135deg, #fff 0%, #f8fafc 100%);
        border-top: 1px solid rgba(48, 96, 255, 0.1);
        box-shadow: 0 -0.125rem 0.625rem rgba(48, 96, 255, 0.05);

        :deep(.hamburger) {
          padding: 0.75rem;
          border-radius: 0.5rem;
          transition: all 0.3s ease;

          &:hover {
            background: rgba(48, 96, 255, 0.1);
            transform: scale(1.1);
          }
        }
      }
    }

    .layout__main {
      flex: 1;
      min-width: 0;
      height: 100%;
      margin-left: 0;
      overflow-y: auto;
      background: #f8fafc;
    }
  }
}

/* 移动端样式 */
:deep(.mobile) {
  .layout__container {
    height: 100%;

    .layout__sidebar--left {
      position: fixed;
      top: $navbar-height;
      bottom: 0;
      left: 0;
      z-index: 1000;
      transition: transform 0.28s;
    }
  }

  &.hideSidebar {
    .layout__sidebar--left {
      width: $sidebar-width !important;
      transform: translateX(-$sidebar-width);
    }
  }
}

:deep(.hasTagsView) {
  .app-main {
    height: calc(100vh - $navbar-height - $tags-view-height) !important;
  }
}
</style>
